import axios from "axios";
import ElementUI from "element-ui";
import VueRouter from "./router/index.js";
axios.defaults.headers.post["Content-Type"] = "application/json";
axios.defaults.headers.get["Content-Type"] = "application/json";
axios.defaults.withCredentials = true;

const path = "/webrtc";

export default {
  hPost(url, params, callback, errorCallBack) {
    axios
      .post(path + url, params)
      .then(response => {
        if (response.data.status === 458) {
          ElementUI.Message.error(response.data.msg);
          VueRouter.replace("/");
          return false;
        }
        if (response.data.status === 459) {
          ElementUI.Message.error(response.data.msg);
          VueRouter.replace("/");
          return false;
        }
        if (typeof callback === "function") {
          callback(response.data);
        }
      })
      .catch(error => {
        if (error.response) {
          if (error.response.status === 500) {
            // sever error
            ElementUI.Notification.error({
              title: "错误",
              duration: 0,
              message: "服务器异常,按F12查看详情!"
            });
            // console.log(error.response.data.exception);
          }
        } else if (error.request) {
          console.log(error.request);
        } else {
          console.log("Error", error);
        }
        if (typeof errorCallBack === "function") {
          errorCallBack();
        }
      });
  },
  formPost(url, params, callback, errorCallBack) {
    axios
      .post(path + url, params, {
        headers: { "Content-Type": "multipart/form-data" }
      })
      .then(response => {
        if (response.data.status === 458) {
          ElementUI.Message.error("账号状态异常请重新登陆，状态码:458");
          VueRouter.replace("/");
          return false;
        }
        if (response.data.status === 459) {
          ElementUI.Message.error("账号状态异常请重新登陆，状态码:459");
          VueRouter.replace("/");
          return false;
        }
        if (typeof callback === "function") {
          callback(response.data);
        }
      })
      .catch(error => {
        if (error.response) {
          if (error.response.status === 500) {
            // sever error
            ElementUI.Notification.error({
              title: "错误",
              duration: 0,
              message: "服务器异常,按F12查看详情!"
            });
            // console.log(error.response.data.exception);
          }
        } else if (error.request) {
          console.log(error.request);
        } else {
          console.log("Error", error);
        }
        if (typeof errorCallBack === "function") {
          errorCallBack();
        }
      });
  },

  hGet(url, params, callback, errorCallBack) {
    axios
      .get(path + url, params)
      .then(response => {
        if (response.data.status === 458) {
          ElementUI.Message.error(response.data.msg);
          VueRouter.replace("/");
          return false;
        }
        if (response.data.status === 459) {
          ElementUI.Message.error(response.data.msg);
          VueRouter.replace("/");
          return false;
        }
        if (typeof callback === "function") {
          callback(response.data);
        }
      })
      .catch(error => {
        if (error.response) {
          if (error.response.status === 500) {
            // sever error
            ElementUI.Notification.error({
              title: "错误",
              duration: 0,
              message: "服务器异常,按F12查看详情!"
            });
            console.log(error.response.data.exception);
          }
        } else if (error.request) {
          console.log(error.request);
        } else {
          console.log("Error", error);
        }
        if (typeof errorCallBack === "function") {
          errorCallBack();
        }
      });
  }
};
